#!/bin/bash
#下面做的是 mysql 同步到 elasticsearch
#需要先在 mysql 上面执行：GRANT select ON `canal-test`.* to 'slave'@'%' ;
#如果需要 同时同步到mysql ，需要再次执行stop slave;change master to.....，
#======================================install canal-server======================================

docker pull canal/canal-server:v1.1.5

docker run -d --name canal-server --restart=always \
--net mynet \
--ip 172.18.0.5 \
-p 11111:11111 \
canal/canal-server:v1.1.5

canalPath=/opt/docker/canal
canalserverPath=$canalPath/canal-server
canalserverSourceConfPath=canal-server:/home/admin/canal-server/conf
canalserverSourceFile=$canalserverSourceConfPath/canal.properties
canalserverTargetFile=$canalserverPath/canal.properties

mkdir -p $canalPath
mkdir -p $canalserverPath

docker cp $canalserverSourceFile $canalserverPath

sed -i 's/canal.destinations = example/canal.destinations = test/' $canalserverTargetFile
docker cp $canalserverTargetFile $canalserverSourceFile

testPath=$canalserverPath/test
mkdir -p $testPath


docker cp $canalserverSourceConfPath/example/instance.properties $testPath
sed -i 's/# canal.instance.mysql.slaveId=0/canal.instance.mysql.slaveId=2/' $testPath/instance.properties
sed -i 's/canal.instance.master.address=127.0.0.1:3306/canal.instance.master.address=172.18.0.2:3306/' $testPath/instance.properties
sed -i 's/canal.instance.dbUsername=canal/canal.instance.dbUsername=slave/' $testPath/instance.properties
sed -i 's/canal.instance.dbPassword=canal/canal.instance.dbPassword=slave123456/' $testPath/instance.properties
docker cp $testPath/ $canalserverSourceConfPath

docker restart canal-server

#docker exec -it canal-server /bin/bash


#======================================install canal-adapter======================================

softwarePath=/opt/software
tar -zxvf $softwarePath/jdk-8u311-linux-x64.tar.gz -C /usr/local/

cat >> /etc/bashrc <<EOF

##############################java env config##############################
export JAVA_HOME=/usr/local/jdk1.8.0_311
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib/dt.JAVA_HOME/lib/tools.jar:\${JRE_HOME}/lib
export PATH=\${JAVA_HOME}/bin:\${PATH}
##############################java env config##############################

EOF

source /etc/bashrc

canalAdapterPath=$canalPath/canal-adapter/
mkdir -p $canalAdapterPath

tar -zxvf $softwarePath/canal.adapter-1.1.6-SNAPSHOT.tar.gz -C $canalAdapterPath

canalAdapterConfigFile=$canalAdapterPath/conf/application.yml

sed -i '94,104d' $canalAdapterConfigFile
sed -i 's/#      - name: es/      - name: es7/' $canalAdapterConfigFile
sed -i 's/#        hosts: 127.0.0.1:9300 # 127.0.0.1:9200 for rest mode/        hosts: http:\/\/127.0.0.1:9200 # 127.0.0.1:9200 for rest mode/' $canalAdapterConfigFile
sed -i 's/#        properties:/        properties:/' $canalAdapterConfigFile
sed -i 's/#          mode: transport # or rest/          mode: rest # or transport/' $canalAdapterConfigFile
sed -i 's/#          # security.auth: test:123456 #  only used for rest mode/          security.auth: elastic:123456 #  only used for rest mode/' $canalAdapterConfigFile
sed -i 's/#          cluster.name: elasticsearch/          cluster.name: docker-cluster/' $canalAdapterConfigFile

sed -i '60,87d' $canalAdapterConfigFile

sed -i 's/  - instance: example # canal instance Name or mq topic name/  - instance: test # canal instance Name or mq topic name/' $canalAdapterConfigFile
sed -i 's/#  srcDataSources:/  srcDataSources:/' $canalAdapterConfigFile
sed -i 's/#    defaultDS:/    defaultDS:/' $canalAdapterConfigFile
sed -i 's/#      url: jdbc:mysql:\/\/127.0.0.1:3306\/mytest?useUnicode=true/      url: jdbc:mysql:\/\/127.0.0.1:3301\/canal-test?useUnicode=true\&characterEncoding=utf8\&autoReconnect=true\&useSSL=false/' $canalAdapterConfigFile
sed -i 's/#      username: root/      username: slave/' $canalAdapterConfigFile
sed -i 's/#      password: 121212/      password: slave123456/' $canalAdapterConfigFile

sed -i '25,47d' $canalAdapterConfigFile


cp $canalAdapterPath/conf/es7/customer.yml $canalAdapterPath/conf/es7/product.yml

productFile=$canalAdapterPath/conf/es7/product.yml

sed -i '15,46d' $productFile
sed -i '11d' $productFile
sed -i '7,9d' $productFile
sed -i 's/destination: example/destination: test/' $productFile
sed -i 's/  _index: customer/  _index: test_product/' $productFile
sed -i 's/  _id: id/  _id: _id/' $productFile
sed -i 's/  sql: \"select t.id, t.name, t.email from customer t\"/  sql: \"SELECT p.id AS _id, p.title, p.sub_title, p.price, p.pic FROM product p \"/' $productFile

sh $canalAdapterPath/bin/startup.sh

sleep 2s

tail -f $canalAdapterPath/logs/adapter/adapter.log

#curl -X POST http://127.0.0.1:8081/etl/es7/product.yml
# 说明：每次加入一个同步文件，必须重启，先stop，然后start

#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 5, '小米8', ' 全面屏游戏智能手机 6GB+64GB', 1999.00, NULL );
#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 6, '小米9', '9 全面屏游戏智能手机 6GB+64GB', 2999.00, NULL );
#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 7, '小米10', '10 全面屏游戏智能手机 6GB+64GB', 3999.00, NULL );




##https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

#https://juejin.cn/post/6891435372824395784

#https://www.jianshu.com/p/2c704f5e7ae1



#docker pull canal/canal-admin:v1.1.5
